<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<div th:insert="/common.html"></div>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  >									
									<button class="btn btn-default" >查询</button>                                    
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
<!--			                      <thead>-->
<!--			                          <tr>-->
<!--			                              <th class="" style="padding-right:0px">-->
<!--			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">-->
<!--			                              </th> -->
<!--										  <th class="sorting_asc">规格ID</th>-->
<!--									      <th class="sorting">规格名称</th>									     												-->
<!--					                      <th class="text-center">操作</th>-->
<!--			                          </tr>-->
<!--			                      </thead>-->
<!--			                      <tbody>-->
<!--			                          <tr >-->
<!--			                              <td><input  type="checkbox" ></td>			                              -->
<!--				                          <td>1</td>-->
<!--									      <td>网络制式</td>-->
<!--		                                  <td class="text-center">                                           -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
<!--									  <tr >-->
<!--			                              <td><input  type="checkbox" ></td>			                              -->
<!--				                          <td>2</td>-->
<!--									      <td>服装尺码</td>-->
<!--		                                  <td class="text-center">                                           -->
<!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           -->
<!--		                                  </td>-->
<!--			                          </tr>-->
<!--			                      </tbody>-->
			                  </table>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td>
						<input  class="form-control" name="specName" placeholder="规格名称" >
					</td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" onclick="addRow()" class="btn btn-default" title="新建" ><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table id="tab" class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
                          <th class="sorting">规格选项</th>
                          <th class="sorting">排序</th>
                          <th class="sorting">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                      <tr>
				            <td>
				            	<input  class="form-control" name="optionName" placeholder="规格选项">
				            </td>
				            <td>
				            	<input  class="form-control" name="orders" placeholder="排序">
				            </td>
							<td>
								<button type="button" class="btn btn-default" onclick="dele(this)" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
                    </tbody>
			  </table>
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="insertGet()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
</body>
<script type="text/javascript">
	$('#dataList').bootstrapTable({
		url: "/specification/selectInfoLimitServer", //请求后台的URL（*）
		method: 'get',                      //请求方式（*）
		// contentType: "application/x-www-form-urlencoded",/*使用post*/
		// toolbar: '#sdfname',              //工具按钮用哪个容器
		striped: true,                      //是否显示行间隔色
		cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
		pagination: true,                   //是否显示分页（*）
		sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
		pageNumber: 1,                      //初始化加载第一页，默认第一页,并记录
		pageSize: 5,                        //每页的记录行数（*）
		pageList: [1, 3, 5],                //可供选择的每页的行数（*）
		showColumns: true,                  //是否显示所有的列（选择显示的列）
		showRefresh: true,                  //是否显示刷新按钮
		minimumCountColumns: 1,             //最少允许的列数
		clickToSelect: true,                //是否启用点击选中行
		showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
		detailView: true,                  //是否显示父子表
		//得到查询的参数
		queryParams : function (params) {
			//这里的键的名字和控制器的变量名必须一致，这边改动，控制器也需要改成一样的
			var temp = {
				pageSize: params.limit,
				//页面大小
				pageNumber: (params.offset / params.limit) + 1 ,  //页码
			};
			return temp;
		},
		columns: [{
			checkbox: true,
			visible: true                  //是否显示复选框
		}, {
			field: 'id',
			title: '规格ID'
		}, {
			field: 'specName',
			title: '规格名称'
		},{
			field:'id',
			title: '操作',
			width: 120,
			align: 'center',
			valign: 'middle',
			formatter: function (value, row, index) {
				var id = value;
				var result = "";
				result += "<a href='javascript:EditViewById(" + id + ")' class='btn btn-xs blue' title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
				result += "<a href='javascript:DeleteById(" + id + ")' class='btn btn-xs red' title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
				return result;
			}
		}
		],
		onLoadError: function () {
			toastr.error("数据加载失败！");
		},onDblClickRow: function (row, $element) {//双击事件
		}
	})
	/*查询*/
	function query() {
		$("#dataList").bootstrapTable("refresh");
	}

    function  addRow(){
        var tab = document.getElementById("tab");
        //alert(tab.rows.length)
        var rownum = tab.rows.length;// 行数
        var newRow=tab.insertRow(rownum);

        var htmlTab = newRow.insertCell(0);
        var html  = "";
            html  +='<tbody><tr>';
            html  += '<td><input  class="form-control" name="optionName" placeholder="规格选项"></td>';
            html  += '<td><input  class="form-control" name="orders" placeholder="排序"></td>';
            html  += '<td><button type="button" class="btn btn-default" onclick="dele(this)" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td>';
            html  += '</tr></tbody>';
        newRow.innerHTML = html;
    }
    function dele(obj){
        var otr = obj.parentNode.parentNode;
        var tab =  otr.parentNode; //自己的父节点 就是table
        tab.removeChild(otr);
    }
    /*添加*/
    function insertGet() {
        let optionNameArr = [];
        $("input[name='optionName']").each(function () {
            optionNameArr.push($(this).val());
        });
        let ordersArr = [];
        $("input[name='orders']").each(function () {
            ordersArr.push($(this).val());
        });
        console.log(ordersArr);
        $.ajax({
            url:"/specification/addSpecification",
            type:"post",
            dataType:"json",
            data:{
                specName:$("[name=specName]").val(),
                optionNames:optionNameArr,
                orders:ordersArr
            },
            async:true,
            success:function (result) {
                if (result.code == 10000){
                    $("#dataList").bootstrapTable("refresh",{pageNumber:1});
                    toastr.success("成功");
                }else if (result.code==9999){
                    toastr.error("失败");
                }
            },
            error:function (result) {
                toastr.error("错误");
            }
        })
    }
    /*修改*/
    function EditViewById(id){
        var titleName = "修改";
        var pageUrl = "/specification/runUpdateSpecification/" + id;
        var executeUrl = "/specification/updateSpecification";
        BootstrapDialog.show({
            title: titleName,
            type: BootstrapDialog.TYPE_DEFAULT,
            size: BootstrapDialog.SIZE_WIDE,
            closeable: true,
            message: $('<div></div>').load(pageUrl),
            buttons: [{
                label: titleName,
                cssClass: "btn btn-success",//设置按钮样式
                action: function (dialog) {//点击修改事件
                    $.ajax({
                        url:executeUrl,
                        type:"post",
                        dataType:"json",
                        data:$("#updateTable").serialize(),
                        async:true,
                        success:function (result) {
                            if (result.code == 10000){
                                toastr.success(titleName+"成功");
                                dialog.close();
                            }else if (result.code == 9999){
                                toastr.error(titleName+"失败");
                            }
                        },
                        error:function (result) {
                            toastr.error("错误");
                        }
                    })
                }
            }, {
                label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            }]
        })
    }
    /*删除*/
    function DeleteById(id) {
        var titleName = "删除";
        if (confirm("确定要删除吗?")) {
            $.ajax({
                url: "/specification/deleteSpecification/" + id,
                type: "post",
                dataType: "json",
                data: {},
                async: false,
                success: function (result) {
                    if (result.code == 10000) {
                        toastr.success(result.message);
                        $("table").bootstrapTable("refresh", {pageNumber: 1});
                    } else {
                        toastr.error(result.message);
                    }
                },
                error: function (result) {
                    toastr.error("错误");
                }
            })
        }
    }
</script>
</html>